﻿namespace Sidvall.CodeGenerator
{
    public class ResourceFileCollection : System.Collections.ObjectModel.KeyedCollection<string, ResourceFile>
    {
        #region Business Members

        #region Add

        public ResourceFile Add(string fileName)
        {
            return Add(fileName, null, null);
        }
        public ResourceFile Add(string fileName, string domainObjectName, string domainObjectTag)
        {
            if (string.IsNullOrWhiteSpace(domainObjectName))
            {
                domainObjectName = TypeMapper.GetSingularName(System.IO.Path.GetFileNameWithoutExtension(fileName));
                domainObjectName += "Manager";
                domainObjectName = TypeMapper.ConvertCaseString(domainObjectName, CaseType.PascalCase);
            }
            if ((string.IsNullOrWhiteSpace(domainObjectTag)) && (domainObjectName.EndsWith("ResourceManager")) && (domainObjectName != "ResourceManager"))
            {
                domainObjectTag = domainObjectName.Substring(0, domainObjectName.Length - "ResourceManager".Length);
            }
            var item = new ResourceFile()
            {
                FileName = fileName,
                DomainObjectName = domainObjectName,
                DomainObjectTag = domainObjectTag,
            };
            Add(item);
            return item;
        }

        #endregion
        #region AddFromFolder

        public void AddFromFolder(string path)
        {
            AddFromFolder(path, null);
        }
        public void AddFromFolder(string path, string searchPattern)
        {
            if (string.IsNullOrWhiteSpace(searchPattern))
                searchPattern = "*Resources.resx";
            var fileNames = System.IO.Directory.GetFileSystemEntries(path, searchPattern, System.IO.SearchOption.TopDirectoryOnly);
            foreach (var fileName in fileNames)
                Add(fileName);
        }

        #endregion
        #region GetKeyForItem

        protected override string GetKeyForItem(ResourceFile item)
        {
            if (item == null)
                throw new System.ArgumentNullException("item");

            return item.DomainObjectName;
        }

        #endregion

        #endregion
        #region Constructors

        public ResourceFileCollection()
            : base(System.StringComparer.OrdinalIgnoreCase)
        {
        }

        #endregion
    }
}
